#! /usr/bin/env python
# coding=utf-8
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# Author: senlin
# Create: 2021-05-27
# ******************************************************************************/

import sys
# sys.path.append('/home/oect')
import ast
import os
from src.libs.logger import logger

def find_functions_without_return(file_path, functions_without_return:list):
    with open(file_path, 'r') as f:
        tree = ast.parse(f.read())
    # functions_without_return = []
    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef):
            if not any(isinstance(n, ast.Return) for n in node.body):
                functions_without_return.append((file_path, node.name))
    return functions_without_return

def find_functions_without_return_in_folder(folder_path):
    functions_without_return = []
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith('.py'):
                file_path = os.path.join(root, file)
                functions_without_return = find_functions_without_return(file_path, functions_without_return)
                # if functions_without_return_in_file:
                #     functions_without_return.append((file_path, functions_without_return_in_file))

    return functions_without_return

def check_no_return_func(folder_path):
    functions_without_return = find_functions_without_return_in_folder(folder_path)
    for file_path, functions in functions_without_return:
        logger.info(f'Functions without return in {file_path}: {functions}')
    # save_to_csv('result.csv', functions_without_return)
    return functions_without_return